<template>
  <el-dialog
    title="上传报告"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form  :model="MonitorReport" label-width="100px" class="demo-form-inline">
      <el-form-item label="报告编号">
        <el-input v-model="MonitorReport.reportNo"></el-input>
      </el-form-item>
      <el-form-item label="报告类型">
        <el-select style="width:100%" v-model="MonitorReport.reportType" placeholder="请选择报告类型">
            <el-option label="周报"  value="1"></el-option>
            <el-option label="月报"  value="2"></el-option>
            <el-option label="预警公文"  value="3"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="报告时间">
          <el-date-picker
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            v-model="MonitorReport.createTime"
            style="width: 100%;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="报告附件">
        <el-upload
          name="file"
          class="upload-demo"
          action="string"
          :http-request="beforeAvatarUploadThree"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-success="handlesuccess"
          :on-error="handleerror"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <span  v-if="uploadfileerror">上传错误重新上传</span>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="create">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from '../../../api'
import qs from 'qs'
export default {
  data() {
    return {
      uploadfileerror:false,
      dialogVisible: false,
      //报告管理
      MonitorReport: {
        tId: '',
        reportType:'',
        reportNo: '',
        createTime:'',
        resources:''
      },
      fileList:[]
    }
  },
  mounted() {
    this.$bus.$on('onAddMonitorReportShow', (flag) => {
      if (this.MonitorReport.tId == '') {
        this.$message({
          type: 'warning',
          message: '请选择隧道!'
        })
        this.dialogVisible = false
      }else{
        this.dialogVisible = true
      }
    })
    this.$bus.$on('onAddMonitorReportTid', (flag) => {
      this.MonitorReport.tId = flag
    })
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    create() {
      api
        .monitorReportsave(qs.stringify(this.MonitorReport))
        .then((res) => {
          if (res.data.code == 2001) {
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.dialogVisible = false
            this.$bus.$emit('onRefreshMonitorReport', this.MonitorReport.tId)
          } else {
            this.$message({
              type: 'fail',
              message: res.data.message
            })
            console.log(res.data.message)
          }
        })
        .catch((err) => {})
    },
    handlePreview(file) {
      console.log(file)
    },
    handlesuccess(response, file, fileList) {
      console.log(22222222233333)
      if (response.code == 2001) {
        this.MonitorReport.resources = response.data
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleerror(err, file, fileList) {

      console.log("11111")
      if (err.data.code != 2001){
        this.uploadfileerror = true
      }
        
    },
    beforeAvatarUploadThree(file) {
      let params = new FormData()
      params.append('file', file.file)

      api
        .AddrPlaneFileUpload(params)
        .then((res) => {
          console.log(111)
          if (res.data.code == 2001) {
            this.MonitorReport.resources = res.data.data 
            this.uploadfileerror = false 
          }
        }).catch(err=>{
          console.log(err)
          this.uploadfileerror = true
        })

    }
  }
}
</script>

<style>
</style>